package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.mysema.query.BooleanBuilder;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class PersonWeeklyReportService {

	@Autowired
	private PersonWeeklyReportDao personWeeklyReportDao;

	private QPersonWeeklyReport $ = QPersonWeeklyReport.personWeeklyReport;

	@Autowired
	public void setPersonWeeklyReportDao(
			PersonWeeklyReportDao personWeeklyReportDao) {
		this.personWeeklyReportDao = personWeeklyReportDao;
	}

	public List<PersonWeeklyReport> findAll() {
		return ListUtil.toList(personWeeklyReportDao.findAll());
	}

	public List<PersonWeeklyReport> findAll(Date beginDateBegin,
			Date beginDateEnd, Date endDateBegin, Date endDateEnd,
			String workDescription, String workPlan, Date createTimeBegin,
			Date createTimeEnd, Long personId, String orderByAttribute) {
		BooleanBuilder condition = new BooleanBuilder();

		if (beginDateBegin != null) {
			condition.and($.beginDate.after(beginDateBegin));
		}

		if (beginDateEnd != null) {
			condition.and($.beginDate.before(beginDateEnd));
		}

		if (endDateBegin != null) {
			condition.and($.endDate.after(endDateBegin));
		}

		if (endDateEnd != null) {
			condition.and($.endDate.before(endDateEnd));
		}

		if (StringUtil.isNotEmpty(workDescription)) {
			condition.and($.workDescription.contains(workDescription));
		}

		if (StringUtil.isNotEmpty(workPlan)) {
			condition.and($.workPlan.contains(workPlan));
		}

		if (createTimeBegin != null) {
			condition.and($.createTime.after(createTimeBegin));
		}

		if (createTimeEnd != null) {
			condition.and($.createTime.before(createTimeEnd));
		}

		if (IdUtil.isNotEmpty(personId)) {
			condition.and($.person.id.eq(personId));
		}

		return ListUtil.toList(personWeeklyReportDao.findAll(condition));
	}

	public Page<PersonWeeklyReport> findAll(Date beginDateBegin,
			Date beginDateEnd, Date endDateBegin, Date endDateEnd,
			String workDescription, String workPlan, Date createTimeBegin,
			Date createTimeEnd, Long personId, int pageNumber, int pageSize,
			String sortAttribute, String orderDirection) {
		BooleanBuilder condition = new BooleanBuilder();

		if (beginDateBegin != null) {
			condition.and($.beginDate.after(beginDateBegin));
		}

		if (beginDateEnd != null) {
			condition.and($.beginDate.before(beginDateEnd));
		}
		if (endDateBegin != null) {
			condition.and($.endDate.after(endDateBegin));
		}

		if (endDateEnd != null) {
			condition.and($.endDate.before(endDateEnd));
		}
		if (StringUtil.isNotEmpty(workDescription)) {
			condition.and($.workDescription.contains(workDescription));
		}
		if (StringUtil.isNotEmpty(workPlan)) {
			condition.and($.workPlan.contains(workPlan));
		}
		if (createTimeBegin != null) {
			condition.and($.createTime.after(createTimeBegin));
		}

		if (createTimeEnd != null) {
			condition.and($.createTime.before(createTimeEnd));
		}
		if (IdUtil.isNotEmpty(personId)) {
			condition.and($.person.id.eq(personId));
		}

		Sort sortObject = null;
		if (StringUtil.isEmpty(sortAttribute)) {
			sortObject = new Sort(Direction.DESC, "id");
		} else {
			sortObject = new Sort(Direction.fromStringOrNull(orderDirection),
					sortAttribute);
		}

		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize,
				sortObject);
		return personWeeklyReportDao.findAll(condition, pageRequest);
	}

	public PersonWeeklyReport getPersonWeeklyReport(Long id) {
		return personWeeklyReportDao.getOne(id);
	}

	public void deletePersonWeeklyReport(Long id) {
		personWeeklyReportDao.delete(id);
	}

	public void savePersonWeeklyReport(PersonWeeklyReport personWeeklyReport) {
		if(null == personWeeklyReport.getCreateTime()){
			personWeeklyReport.setCreateTime(DateUtil.nowDate());
		}
		personWeeklyReportDao.save(personWeeklyReport);
	}

	public PersonWeeklyReport findOne(Long id) {
		return this.personWeeklyReportDao.findOne(id);
	}

}
